/*
 * Copyright ShangHai ECLUB Information Technology Co.,Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.eclub.emotion.rootstone.server.util;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.eclub.emotion.rootstone.serializer.scanning.FilesScanner;

/**
 * 
 *
 * @author   jun.yang, Wincor Nixdorf International GmbH
 */
public class ClassTools {
	public final static String CLASS_FILE_SUFFIX = ".class";
	public static List<Class<?>> loadClass(String[] directory, ClassLoader classLoader) throws ClassNotFoundException {
		FilesScanner fs = new FilesScanner(directory);
		Set<Class<?>> clazzSet = new LinkedHashSet<Class<?>>();
		for (; fs.hasNext();) {
			String classFilePath = fs.next();
			if (classFilePath.endsWith(CLASS_FILE_SUFFIX)) {
				String className = classFilePath.replaceAll(CLASS_FILE_SUFFIX, "").replaceAll("/", ".");
				Class<?> clazz = null;
				clazz = classLoader.loadClass(className);
				if (clazz != null) {
					clazzSet.add(clazz);
				}
			}
		}
		return new LinkedList<Class<?>>(clazzSet);
	}
}


